package com.qeado.admin.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qeado.admin.dao.RoleDao;
import com.qeado.admin.model.Menu;
import com.qeado.admin.model.Role;
import com.qeado.admin.service.RoleService;

@Service
public class RoleServiceImpl implements RoleService {
	@Autowired
	private RoleDao roleDao;

	public void deleteRole(String roleId) {
		roleDao.deleteRole(roleId);
	}

	public String saveRole(Role roleInfo, String[] menuIds) {
		if(roleInfo.getId() != null && !"".equals(roleInfo.getId())) {
			//roleDao.updateRole(roleInfo);
			if(menuIds != null && menuIds.length > 0) {
				Map<String, String> map = new HashMap<String, String>();
				map.put("roleId", roleInfo.getId());
				roleDao.deleteRoleMenu(map);
				map.put("creator", roleInfo.getCreator());
				for(int i=0;i<menuIds.length;i++) {
					map.put("id", UUID.randomUUID().toString());
					map.put("menuId", menuIds[i]);
					roleDao.saveRoleMenu(map);
				}
			}
		} else {
			Object obj = roleDao.searchRoleByName(roleInfo);
			if(obj != null)
				return "该角色名称已存在！";
			roleInfo.setId(UUID.randomUUID().toString());
			roleDao.saveRole(roleInfo);
		}
		return null;
	}

	public List<Role> searchRole(Role roleInfo) {
		return roleDao.searchRole(roleInfo);
	}

	public Role selectRole(String roleId) {
		return roleDao.selectRole(roleId);
	}

	public List<Menu> searchRoleMenuList(String roleId) {
		return roleDao.searchRoleMenuList(roleId);
	}

}
